package edu.hit.pact.pgse.timer;

import java.util.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.hit.pact.pgse.util.Globals;

/*
 * need to schedule four kinds of tasks,
 * first one is to reload the conf.properity into Globals every hour
 * second is to schedule the web task on 0:00
 * third is to run the blog task on 0:00
 * forth is to run the bbs task from 0:00 every hour
 */

public class ConfTaskScheduler {
	protected Timer confTaskTimer;
	private Log logger;
	
	public ConfTaskScheduler(){
		confTaskTimer = new Timer();
		logger = LogFactory.getLog(ConfTaskScheduler.class);
	}
	
	public void scheduleConfTask(){
		ConfReloadTimerTask confReloadTimerTask = new ConfReloadTimerTask(this);
		
		confTaskTimer.scheduleAtFixedRate(confReloadTimerTask, 600000, 600000);
		logger.info("scheduleConfReload");
	}
	
	public void reScheduleTask(String type){
		if (type.equals("conf")){
			logger.error("conf update interval reschedule");
			confTaskTimer.cancel();
			confTaskTimer = new Timer();
			scheduleConfTask();
		} else {
			logger.error("type wrong");
		}
	}
	
	public static void main(String[] args){
		/*
		 * load log4j property.
		 */
		Globals.readLog4jProperties();
		(new ConfTaskScheduler()).scheduleConfTask();
	}
}
